<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>集合框架 | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/24.c1af1909.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础高级</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/0c7696/" aria-current="page" class="active sidebar-link">集合框架</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_1-1-今日学习内容" class="sidebar-link">1.1 今日学习内容</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_1-2-今日学习目标" class="sidebar-link">1.2 今日学习目标</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#二、数据结构概述" class="sidebar-link">二、数据结构概述</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_2-1-什么是数据结构-了解" class="sidebar-link">2.1 什么是数据结构（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_2-2-为什么要熟悉数据结构-了解" class="sidebar-link">2.2 为什么要熟悉数据结构（了解）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#三、数据结构的特点" class="sidebar-link">三、数据结构的特点</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_3-1-数组" class="sidebar-link">3.1 数组</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_3-2-链表" class="sidebar-link">3.2 链表</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_3-3-队列" class="sidebar-link">3.3 队列</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_3-3-栈" class="sidebar-link">3.3 栈</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#四、集合框架体系" class="sidebar-link">四、集合框架体系</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_4-1-集合框架概述-了解" class="sidebar-link">4.1 集合框架概述（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_4-2-集合的分类-掌握" class="sidebar-link">4.2 集合的分类（掌握）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#五、list接口" class="sidebar-link">五、List接口</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_5-1-list常用api方法-必须记住" class="sidebar-link">5.1 List常用API方法（必须记住）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_5-2-arraylist类-重点" class="sidebar-link">5.2 ArrayList类（重点）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_5-3-linkedlist类-了解" class="sidebar-link">5.3 LinkedList类（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_5-4-vector类-了解" class="sidebar-link">5.4 Vector类（了解）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#六、泛型" class="sidebar-link">六、泛型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_6-1-使用泛型的好处-了解" class="sidebar-link">6.1 使用泛型的好处（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_6-2-自定义和使用泛型类-了解" class="sidebar-link">6.2 自定义和使用泛型类（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_6-3-在集合框架中使用泛型-掌握" class="sidebar-link">6.3 在集合框架中使用泛型（掌握）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#七、集合元素迭代" class="sidebar-link">七、集合元素迭代</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_7-1-集合元素遍历-掌握" class="sidebar-link">7.1 集合元素遍历（掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_7-2-并发修改异常-了解" class="sidebar-link">7.2 并发修改异常（了解）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#八、set接口" class="sidebar-link">八、Set接口</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_8-1-hashset类" class="sidebar-link">8.1 HashSet类</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_8-1-1-对象的哈希值" class="sidebar-link">8.1.1 对象的哈希值</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_8-1-2-哈希表结构工作过程" class="sidebar-link">8.1.2 哈希表结构工作过程</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_8-2-treeset类-了解" class="sidebar-link">8.2 TreeSet类（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_8-3-comparable接口-知道" class="sidebar-link">8.3 Comparable接口（知道）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_8-4-comparator接口-知道" class="sidebar-link">8.4 Comparator接口（知道）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#九、map接口" class="sidebar-link">九、Map接口</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_9-1-map接口概述-理解" class="sidebar-link">9.1 Map接口概述（理解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_9-2-map常用的api方法-记住" class="sidebar-link">9.2 Map常用的API方法（记住）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_9-3-hashmap-重点" class="sidebar-link">9.3 HashMap（重点）</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_9-4-treemap-了解" class="sidebar-link">9.4 TreeMap（了解）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#十、框架小结和工具类" class="sidebar-link">十、框架小结和工具类</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_10-1-框架小结" class="sidebar-link">10.1 框架小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_10-2-工具类" class="sidebar-link">10.2 工具类</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_10-2-1-工具类的设计" class="sidebar-link">10.2.1 工具类的设计</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_10-2-2-arrays-掌握" class="sidebar-link">10.2.2 Arrays（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_10-2-3-collections-了解" class="sidebar-link">10.2.3 Collections（了解）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/0c7696/#十一、小结与作业" class="sidebar-link">十一、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_11-1-小结" class="sidebar-link">11.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/0c7696/#_11-2-作业" class="sidebar-link">11.2 作业</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-1-题目1" class="sidebar-link">11.2.1 题目1</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-2-题目2" class="sidebar-link">11.2.2 题目2</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-3-题目3" class="sidebar-link">11.2.3 题目3</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-4-题目4" class="sidebar-link">11.2.4 题目4</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-5-题目5" class="sidebar-link">11.2.5 题目5</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-6-题目6" class="sidebar-link">11.2.6 题目6</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-7-题目7" class="sidebar-link">11.2.7 题目7</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-8-题目8" class="sidebar-link">11.2.8 题目8</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-9-题目9" class="sidebar-link">11.2.9 题目9</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-10-题目10" class="sidebar-link">11.2.10 题目10</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-11-题目11" class="sidebar-link">11.2.11 题目11</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-12-题目12" class="sidebar-link">11.2.12 题目12</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-13-题目13" class="sidebar-link">11.2.13 题目13</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-14-题目14" class="sidebar-link">11.2.14 题目14</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-15-题目15" class="sidebar-link">11.2.15 题目15</a></li><li class="sidebar-sub-header level4"><a href="/pages/0c7696/#_11-2-16-题目16" class="sidebar-link">11.2.16 题目16</a></li></ul></li></ul></li><li><a href="/pages/19e70b/" class="sidebar-link">异常机制</a></li><li><a href="/pages/a53030/" class="sidebar-link">多线程</a></li><li><a href="/pages/16b819/" class="sidebar-link">IO</a></li><li><a href="/pages/258730/" class="sidebar-link">单元测试与配置文件</a></li><li><a href="/pages/1fdc1c/" class="sidebar-link">反射与内省机制</a></li><li><a href="/pages/80d6e3/" class="sidebar-link">Lambda表达式</a></li><li><a href="/pages/a53dcc/" class="sidebar-link">Stream流操作</a></li><li><a href="/pages/42948c/" class="sidebar-link">注解</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E5%9F%BA%E7%A1%80%E9%AB%98%E7%BA%A7" title="分类" data-v-06225672>基础高级</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-03</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">集合框架<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="集合框架"><a href="#集合框架" class="header-anchor">#</a> 集合框架</h1> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <h3 id="_1-1-今日学习内容"><a href="#_1-1-今日学习内容" class="header-anchor">#</a> 1.1 今日学习内容</h3> <ul><li><p>常见的数据结构</p></li> <li><p>集合框架体系</p></li> <li><p>List接口和实现类</p></li> <li><p>泛型</p></li> <li><p>集合元素迭代</p></li> <li><p>Set接口和实现类</p></li> <li><p>Map接口和实现类</p></li> <li><p>集合的工具类</p></li></ul> <h3 id="_1-2-今日学习目标"><a href="#_1-2-今日学习目标" class="header-anchor">#</a> 1.2 今日学习目标</h3> <ul><li><p>常见数据结构</p> <ul><li>了解什么是数据结构</li> <li>知道常见的数据结构和其特点</li></ul></li> <li><p>掌握集合框架体系结构</p></li> <li><p>掌握List接口存储特点</p></li> <li><p>掌握ArrayList类常用方法</p></li> <li><p>泛型</p> <ul><li>了解泛型的定义和作用</li> <li>掌握使用集合时使用泛型来约束元素类型</li></ul></li> <li><p>掌握集合元素迭代</p></li> <li><p>掌握Set接口存储特点</p> <ul><li><p>掌握HashSet类常用方法</p></li> <li><p>知道Comparable和Comparator接口</p></li></ul></li> <li><p>掌握Map接口和存储特点</p> <ul><li>掌握HashMap类常用方法</li></ul></li> <li><p>掌握Arrays和Collections工具类的常用方法</p></li></ul> <h2 id="二、数据结构概述"><a href="#二、数据结构概述" class="header-anchor">#</a> 二、数据结构概述</h2> <p>Java的集合框架其实就是对数据结构的封装，在学习集合框架之前，有必要先了解下数据结构。</p> <h3 id="_2-1-什么是数据结构-了解"><a href="#_2-1-什么是数据结构-了解" class="header-anchor">#</a> 2.1 什么是数据结构（了解）</h3> <p>所谓数据结构，其实就是计算机存储、组织数据的方式。</p> <p>数据结构是用来分析研究<strong>数据存储</strong>操作的，其实就是对数据做增删改查操作。</p> <ul><li>增：把某个数据存储到某个容器中</li> <li>删：从容器中把某个数据删除掉</li> <li>改：把容器中某个数据替换成另一个数据</li> <li>查：把容器中的数据查询出来</li></ul> <blockquote><p>意识</p> <p>只要提到容器，那核心操作一定是增删改查。</p></blockquote> <p>不同的数据结构，底层采用不同的存储方式（算法），在具体操作的时候效率是不一样的，比如有的查询速度很快，有的插入速度很快，有的操作头和尾速度很快等。</p> <h3 id="_2-2-为什么要熟悉数据结构-了解"><a href="#_2-2-为什么要熟悉数据结构-了解" class="header-anchor">#</a> 2.2 为什么要熟悉数据结构（了解）</h3> <p>常见的数据结构：</p> <ul><li><strong>数组（Array）				掌握</strong></li> <li>链表（Linked List）		了解</li> <li>哈希表（Hash）			  了解</li> <li>栈（Stack）				     了解</li> <li>队列（Queue）			  了解</li> <li>树（Tree）				      了解</li> <li>图（Graph）</li> <li>堆（Heap）</li></ul> <p>在未来具体业务场景时，我们需要分析具体的需求场景，是查询的操作多，还是添加的操作多？</p> <p>如果是查询操作多，我们就要选择适合查询性能高的数据结构；</p> <p>如果添加操作多，我们就需要选择添加性能高的数据结构。</p> <p><strong>一句话：根据具体业务场景，选择合适的集合类。</strong></p> <h2 id="三、数据结构的特点"><a href="#三、数据结构的特点" class="header-anchor">#</a> 三、数据结构的特点</h2> <h3 id="_3-1-数组"><a href="#_3-1-数组" class="header-anchor">#</a> 3.1 数组</h3> <p>我们在这里针对<strong>数组</strong>存储数据的增删改查（CRUD）做性能分析：</p> <ul><li><p>添加操作：</p> <p>如果在数组的最后一个位置添加元素，需要操作一次。</p> <p>如果添加在数组的第一个位置，如果存在N个元素，此时需要操作N次(元素要整体后移)。</p></li> <li><p>删除操作:</p> <p>如果删除最后一个元素，操作一次。</p> <p>如果删除第一个元素，操作N次(元素要整体前移)。</p></li> <li><p>修改操作:  给定索引时，操作1次</p></li> <li><p>查询操作：根据索引查询元素需要操作1次，根据内容查询最坏的情况下需要N次；</p></li></ul> <p><strong>结论：</strong></p> <p><strong>基于数组的数据结构：</strong></p> <ul><li><strong>(已知索引时)做查询是和修改是非常快的</strong></li> <li><strong>在前面和中间添加和删除操作比较慢（需要移动元素），在末尾添加和删除速度快</strong></li></ul> <p>那如果想保证添加和删除操作的性能，此时就得提提链表这种数据结构了。</p> <h3 id="_3-2-链表"><a href="#_3-2-链表" class="header-anchor">#</a> 3.2 链表</h3> <p>链表结构（火车和火车车厢）：通过引用来表示上一个节点和下一个节点的关系。</p> <p>1)：单向链表，只能从头遍历到尾/只能从尾遍历到头。</p> <p>2)：双向链表，既可以从头遍历到尾,又可以从尾遍历到头。</p> <p>单向链表：</p> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogsimple-one-list.png" alt=""></em></p> <p>双向链表：</p> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogsimple-two-link.png" alt=""></em></p> <p><strong>对链表操作的性能分析：</strong></p> <p>双向链表可以直接获取自己的第一个和最后一个节点。</p> <ul><li><p>添加操作</p> <p>如果新增的元素在第一个或最后一个位置，那么操作只有1次。</p></li> <li><p>删除操作</p> <p>如果删除第一个元素	：操作一次</p> <p>如果删除最后一个元素：操作一次</p> <p>如果删除中间的元素：找到节点之后做删除操作: 1次</p></li> <li><p>修改操作：每次都从头查找，找到后才能修改</p></li> <li><p>查询操作：每次都从头查找</p></li></ul> <p><strong>结论：基于链表的数据结构：</strong></p> <ul><li><strong>做查询和修改是比较慢的（因为每次从头开始查找）</strong></li> <li><strong>添加和删除操作比较快了（不需要移动任何元素）。</strong></li></ul> <h3 id="_3-3-队列"><a href="#_3-3-队列" class="header-anchor">#</a> 3.3 队列</h3> <p>队列是一种特殊的线性表，特殊之处在于它只允许在表的前端（front）进行删除操作，而在表的后端（rear）进行插入操作，队列是一种操作受限制的线性表。</p> <p>进行插入操作的端称为队尾，进行删除操作的端称为队头。</p> <p>单向队列(Queue)：先进先出( FIFO )，只能从队列尾插入数据，只能从队列头删除数据。</p> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blog01-single-queue2.png" alt=""></em></p> <p>双向队列(Deque)：可以从队列尾/头插入数据，也可以从队列头/尾删除数据。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blog02-double-queue.png" alt=""></p> <p><strong>结论：</strong></p> <ul><li><strong>最擅长操作头和尾</strong></li> <li><strong>元素先进先出(First In First Out)</strong></li></ul> <h3 id="_3-3-栈"><a href="#_3-3-栈" class="header-anchor">#</a> 3.3 栈</h3> <p>栈（stack）又名堆栈，它是一种运算受限的线性表，后进先出(Last In First Out)，和手枪弹夹类似。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogstack-demo.png" style="zoom:67%;"></strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/bloggun.png" alt=""></p> <p>​		栈结构仅允许在表的一端进行插入和删除运算，这一端被称为栈顶，相对地，把另一端称为栈底。向一个栈中插入新元素又称作入栈，它是把新元素放到栈顶元素的上面，使之成为新的栈顶元素。从一个栈中删除元素又称作出栈，表示把栈顶元素删除掉，使其相邻的元素成为新的栈顶元素。</p> <p><strong>结论：</strong></p> <ul><li><strong>最擅长在线性表的一端添加和删除操作</strong></li> <li><strong>元素后进先出( Last In First Out )</strong></li></ul> <p>数据结构特点：</p> <ul><li>数组结构 : 查询快 , 增删慢</li> <li>链表结构 : 查询慢 , 增删快</li> <li>栈结构 : 先进后出</li> <li>队列结构 : 先进先出</li></ul> <h2 id="四、集合框架体系"><a href="#四、集合框架体系" class="header-anchor">#</a> 四、集合框架体系</h2> <h3 id="_4-1-集合框架概述-了解"><a href="#_4-1-集合框架概述-了解" class="header-anchor">#</a> 4.1 集合框架概述（了解）</h3> <p><strong>既然数组可以存储多个数据，为什么要出现集合？</strong></p> <ul><li><p>数组的长度是固定的，集合的长度是可变的（自动拓展容量）。</p></li> <li><p>使用Java类封装出一个个容器类，开发者只需要直接调用即可，不用程序员再手写容器类。</p></li></ul> <p>集合是Java中提供的一种容器，可以用来存储多个数据，根据不同存储方式形成的体系结构，就叫做集合框架体系。集合也时常被称为容器。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogcollections-structure-class.png" alt=""></p> <p>容器类很多，但每一种容器类底层拥有不同的底层算法(数据结构)。<strong>集合中存储的数据，叫做元素，元素只能是对象（引用类型）。</strong></p> <p>在Java中，集合操作( 增、删、改、查 )都被设计成接口，被不同底层数据结构的实现类所实现。</p> <h3 id="_4-2-集合的分类-掌握"><a href="#_4-2-集合的分类-掌握" class="header-anchor">#</a> 4.2 集合的分类（掌握）</h3> <p>根据容器的存储特点的不同，可以分成三种情况：</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogcollection-catalog.png" alt=""></strong></p> <ul><li><p>List(列表)：允许记录添加顺序，允许元素重复。</p></li> <li><p>Set(数据集)：不记录添加顺序，不允许元素重复。</p></li> <li><p>Map(映射)：容器中每一个元素都包含一对key和value，key不允许重复，value可以重复。</p></li></ul> <p>注意：List和Set接口继承于Collection接口，Map接口不继承Collection接口。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogimage-20200621130721999.png" alt=""></p> <ul><li><p>Collection接口：泛指广义上集合，主要表示List和Set两种存储方式。</p></li> <li><p><strong>List接口：表示列表，规定了允许记录元素添加顺序，允许元素重复的规则。</strong></p></li> <li><p><strong>Set接口：表示数据集，规定了不记录元素添加顺序，不允许元素重复的规则</strong>。</p></li> <li><p>Map接口：表示映射关系，规定了两个集合映射关系的规范。</p></li></ul> <p><strong>注意：我们使用的容器接口或类都处于java.util包中。</strong></p> <h2 id="五、list接口"><a href="#五、list接口" class="header-anchor">#</a> 五、List接口</h2> <p>​		List接口是Collection接口子接口，List接口定义了一种规则，要求该容器允许<strong>记录元素的添加顺序，也允许元素重复</strong>。那么List接口的实现类都会遵循这一种规范。</p> <p><strong>List集合存储特点：</strong></p> <ul><li><p>允许记录元素的添加先后顺序</p></li> <li><p>允许元素重复</p></li></ul> <p><strong>该接口常用的实现类有：</strong></p> <ul><li><strong>ArrayList类：数组列表，表示数组结构，底层采用数组实现，开发中必用的实现类，重点。</strong></li> <li>LinkedList类：链表，表示双向列表和双向队列结构，采用链表实现，使用不多。</li> <li>Vector类：向量，其实就是古老的ArrayList，采用数组实现，不会用。</li></ul> <p>一般来说，List接口的实现类命名规则：（底层数据结构 + 接口名）例如：ArrayList</p> <h3 id="_5-1-list常用api方法-必须记住"><a href="#_5-1-list常用api方法-必须记住" class="header-anchor">#</a> 5.1 List常用API方法（必须记住）</h3> <p><strong>添加操作</strong></p> <ul><li><font color="red">boolean add(Object e)：将元素添加到列表的末尾 </font></li></ul> <p><strong>删除操作</strong></p> <ul><li><font color="red">Object remove(int index)：从列表中删除指定索引位置的元素,并返回被删除的元素</font></li></ul> <p><strong>修改操作</strong></p> <ul><li><font color="red">Object set(int index, Object ele)：修改列表中指定索引位置的元素，返回被替换的旧元素 </font></li></ul> <p><strong>查询操作</strong></p> <ul><li><font color="red">int size()：返回当前列表中元素个数 </font></li> <li><font color="red">Object get(int index)：查询列表中指定索引位置对应的元素 </font></li></ul> <p>注意：标红的是经常使用的方法。</p> <h3 id="_5-2-arraylist类-重点"><a href="#_5-2-arraylist类-重点" class="header-anchor">#</a> 5.2 ArrayList类（重点）</h3> <p>ArrayList类，基于数组算法的列表，通过查看源代码会发现底层其实就是一个Object数组。</p> <p>需求1：操作List接口常用方法</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayListDemo1</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">// 创建一个默认长度的列表对象</span>
		<span class="token class-name">List</span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;元素数量：&quot;</span> <span class="token operator">+</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 0</span>
        
		<span class="token comment">// 添加操作：向列表中添加4个元素</span>
		list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;Will&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;alex&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;ben&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;Lucy&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token comment">// 查询操作:</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;列表中所有元素：&quot;</span> <span class="token operator">+</span> list<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 输出:[Will, alex, ben, Lucy]</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;元素数量：&quot;</span><span class="token operator">+</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 4</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;第一个元素：&quot;</span> <span class="token operator">+</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Will</span>
		
		<span class="token comment">// 修改操作：把索引为2的元素，替换为wolfcode</span>
		list<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">&quot;wolfcode&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;修改后：&quot;</span><span class="token operator">+</span> list<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//输出:[Will, alex, wolfcode, Lucy]</span>
		
		<span class="token comment">// 删除操作：删除索引为1的元素</span>
		list<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;删除后：&quot;</span><span class="token operator">+</span> list<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//输出:[Will, wolfcode, Lucy]</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p>需求2：创建四个User对象，存储在ArrayList中，分析内存图。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>
     <span class="token comment">//省略两个参数构造器、getter/setter方法、toString方法</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayListDemo2</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">List</span> girls <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token class-name">User</span> p1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;西施&quot;</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		girls<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>p1<span class="token punctuation">)</span><span class="token punctuation">;</span>
		girls<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;王昭君&quot;</span><span class="token punctuation">,</span><span class="token number">19</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		girls<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;貂蝉&quot;</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		girls<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;杨玉环&quot;</span><span class="token punctuation">,</span><span class="token number">21</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>girls<span class="token punctuation">)</span><span class="token punctuation">;</span>
		
        <span class="token comment">// 修改u1对象的名字和年龄</span>
		p1<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">&quot;小施&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		p1<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">68</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>girls<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><p>运行结果（观察变化）：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>[User [name=西施, age=18], User [name=王昭君, age=19], User [name=貂蝉, age=20], User [name=杨玉环, age=21]]
[User [name=小施, age=17], User [name=王昭君, age=19], User [name=貂蝉, age=20], User [name=杨玉环, age=21]]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>内存分析，解释原因：</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogstack-heap.png" alt=""></strong></p> <p>结论：集合类中存储的对象,都存储的是对象的地址。</p> <h3 id="_5-3-linkedlist类-了解"><a href="#_5-3-linkedlist类-了解" class="header-anchor">#</a> 5.3 LinkedList类（了解）</h3> <p>ArrayList类，基于数组结构的列表，通过查看源代码会发现底层其实就是一个Object数组。</p> <p>LinkedList类，基于链表结构的列表，底层采用链表结构，实现了List接口，除此之外，还实现了（双向）队列，栈数据结构。</p> <p>LinkedList 作为队列使用时，常见方法：</p> <ul><li>void addFirst(Object e) 将指定元素插入此列表的开头 （头部入队）。</li> <li>void addLast(Object e) 将指定元素添加到此列表的结尾 （尾部入队）。</li> <li>Object getFirst() 返回此列表的第一个元素 （获取队列头部元素）。</li> <li>Object getLast() 返回此列表的最后一个元素 （获取队列尾部元素）。</li> <li>Object removeFirst()  移除并返回此列表的第一个元素 （头部出队）。</li> <li>Object removeLast()  移除并返回此列表的最后一个元素 （尾部出队）。</li></ul> <p>LinkedList还可以作为栈数据结构使用（ChatGPT）：</p> <ul><li>void push(Object e) 将元素推入此列表所表示的栈 （入栈）。</li> <li>Object pop() 从此列表所表示的栈处弹出一个元素 （出栈）。</li> <li>Object peek() 获取但不移除此列表的头部元素（获取栈顶元素）。</li></ul> <p>LinkedList之所以有这么多方法，是因为自身实现了多种数据结构，而不同的数据结构的操作方法名称不同，在开发中LinkedList使用不是很多，知道存储特点就可以了。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">LinkedListDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">LinkedList</span> queue <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 入队</span>
        queue<span class="token punctuation">.</span><span class="token function">addLast</span><span class="token punctuation">(</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        queue<span class="token punctuation">.</span><span class="token function">addLast</span><span class="token punctuation">(</span><span class="token string">&quot;B&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        queue<span class="token punctuation">.</span><span class="token function">addFirst</span><span class="token punctuation">(</span><span class="token string">&quot;C&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>queue<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// [C,A,B]</span>

        <span class="token comment">// 获取队列的头部和尾部元素</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;获取队列的头部元素:&quot;</span> <span class="token operator">+</span> queue<span class="token punctuation">.</span><span class="token function">getFirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// C</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;获取队列的尾部元素:&quot;</span> <span class="token operator">+</span> queue<span class="token punctuation">.</span><span class="token function">getLast</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// B</span>

        <span class="token comment">// 出队</span>
        <span class="token class-name">Object</span> ele <span class="token operator">=</span> queue<span class="token punctuation">.</span><span class="token function">removeFirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;出队元素:&quot;</span> <span class="token operator">+</span> ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>queue<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// [A,B]</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>程序运行结果：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>[C, A, B]
获取队列的头部元素:C
获取队列的尾部元素:B
出队元素:C
[A, B]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_5-4-vector类-了解"><a href="#_5-4-vector类-了解" class="header-anchor">#</a> 5.4 Vector类（了解）</h3> <p>Vector类：基于数组结构实现的列表，其实就是ArrayList类的前身，和ArrayList的区别在于：</p> <ul><li>Vector 线程安全的，操作效率低</li> <li>ArrayList 线程不安全，操作效率高</li></ul> <h2 id="六、泛型"><a href="#六、泛型" class="header-anchor">#</a> 六、泛型</h2> <h3 id="_6-1-使用泛型的好处-了解"><a href="#_6-1-使用泛型的好处-了解" class="header-anchor">#</a> 6.1 使用泛型的好处（了解）</h3> <ul><li><p>不用泛型带来的问题？</p> <p>如果不使用泛型，从容器中获取出元素，需要做类型强转，也不能限制容器只能存储相同类型的元素。</p></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;B&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> ele <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">)</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>泛型其实就是一种<strong>类型参数</strong>，<strong>主要用于某个类或接口中数据类型不确定时，可以使用一个标识符来表示未知的数据类型</strong>，然后在使用该类或接口时指定该未知类型的真实类型。</p> <h3 id="_6-2-自定义和使用泛型类-了解"><a href="#_6-2-自定义和使用泛型类-了解" class="header-anchor">#</a> 6.2 自定义和使用泛型类（了解）</h3> <p>需求：定义一个类Point，x和y表示横纵坐标，分别使用String、Integer、Double表示坐标类型。</p> <p>如果没有泛型需要设计三个类，如下：</p> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/bloggenerate.png" alt=""></em></p> <p>完成这三个类后发现，只是x、y的数据类型不同而已，其他都一样。如何解决呢？——泛型</p> <p><strong>1 定义泛型类</strong>  使用一个标识符，在类名后加泛型标记，T在类中表示一种未知的数据类型。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// Type =&gt; T</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Point</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">T</span> x<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token class-name">T</span> y<span class="token punctuation">;</span>
     <span class="token comment">// 省略getter/setter</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>2 使用泛型类</strong>  一般在创建对象时，给未知的类型设置一个具体的类型，当没有指定泛型时，默认类型为Object类型。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 使用String作为泛型类型</span>
<span class="token class-name">Point</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> p2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Point</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> x2 <span class="token operator">=</span> p2<span class="token punctuation">.</span><span class="token function">getX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 使用Integer作为泛型类型</span>
<span class="token class-name">Point</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> p3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Point</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Integer</span> x3 <span class="token operator">=</span> p3<span class="token punctuation">.</span><span class="token function">getX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 没有使用泛型，默认类型是Object</span>
<span class="token class-name">Point</span> p1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Point</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Object</span> x1 <span class="token operator">=</span> p1<span class="token punctuation">.</span><span class="token function">getX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>泛型可用到的接口、类、方法中，将数据类型作为参数传递，其实更像是一个数据类型的占位符。</p> <p>画图分析：</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/bloggenerate-demo.png" alt=""></p> <p>注意：这里仅仅是演示泛型类是怎么回事，并不是要求定义类都要使用泛型。</p> <h3 id="_6-3-在集合框架中使用泛型-掌握"><a href="#_6-3-在集合框架中使用泛型-掌握" class="header-anchor">#</a> 6.3 在集合框架中使用泛型（掌握）</h3> <p>拿List接口和ArrayList类举例，查API手册可知：ArrayList是泛型类和List泛型接口</p> <p>定义ArrayList时，定义者不确定未来开发者向容器中添加什么类型的元素，此时使用一个标识符E表示泛型。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// Element =&gt; E</span>
<span class="token keyword">class</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">E</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">{</span> 
	<span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token class-name">E</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token class-name">E</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token keyword">int</span> index<span class="token punctuation">)</span><span class="token punctuation">{</span>  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>此时的E也仅仅是一个占位符，表示元素（Element）的类型，那么当使用容器时确定泛型( 设置具体的数据类型  )就表示该容器只能存储某种类型的数据。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">//只能存储String类型的集合</span>
<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list1<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list1<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 报错</span>
<span class="token class-name">String</span> ele1 <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//只能存储Integer类型的集合</span>
<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> list2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list2<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list2<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>注意：泛型要求前后两个泛型类型必须保持一致，可通过<strong>泛型类型推断</strong>进行简写：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
可以简写为
<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>通过反编译工具，会发现泛型其实是语法糖，也就是说编译之后，泛型就不存在了。</p> <p><strong>错误用法：</strong></p> <ul><li>泛型必须是引用类型，不能是基本数据类型（错误如下）：</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token keyword">int</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token keyword">int</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//编译错误</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>泛型不存在继承的关系（错误如下）：</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">//错误的</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="七、集合元素迭代"><a href="#七、集合元素迭代" class="header-anchor">#</a> 七、集合元素迭代</h2> <h3 id="_7-1-集合元素遍历-掌握"><a href="#_7-1-集合元素遍历-掌握" class="header-anchor">#</a> 7.1 集合元素遍历（掌握）</h3> <p>集合的遍历：把集合中的每一个元素获取出来</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;西施&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;王昭君&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;貂蝉&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;杨玉环&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>使用for遍历</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> index <span class="token operator">&lt;</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> ele <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>使用迭代器遍历</strong></p> <p>Iterator表示迭代器对象( 专门负责遍历集合的元素 )，迭代器中拥有一个指针，默认指向第一个元素之前，</p> <ul><li><p>boolean hasNext()：判断指针后是否存在下一个元素</p></li> <li><p>Object next()：获取指针位置下一个元素，<strong>获取后指针向后移动一位</strong></p></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> it <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name">String</span> ele <span class="token operator">=</span> it<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>操作原理如下图：</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogforeach-demo.png" alt=""></strong></p> <p>使用语法糖 for-each 遍历（推荐使用）</p> <p>语法：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">for</span><span class="token punctuation">(</span>数据类型 迭代变量<span class="token operator">:</span> 数组<span class="token operator">/</span>单元素集合<span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token comment">// 使用迭代变量，迭代变量中存储了每次遍历的集合元素</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>注意</strong>：单元素集合是指Collection集合，不包括Map集合。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> ele <span class="token operator">:</span> list<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>通过反编译工具会发现，for-each操作集合时，其实底层依然是Iterator，我们直接使用for-each即可。</p> <h3 id="_7-2-并发修改异常-了解"><a href="#_7-2-并发修改异常-了解" class="header-anchor">#</a> 7.2 并发修改异常（了解）</h3> <p>需求：在迭代集合时删除集合元素，比如删除王昭君。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;西施&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;王昭君&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;貂蝉&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;杨玉环&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 在迭代过程中进行删除操作</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> ele <span class="token operator">:</span> list<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token string">&quot;王昭君&quot;</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		list<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>此时报错java.util.ConcurrentModificationException，并发修改异常。</p> <p>造成该错误的原因是，<strong>不允许在迭代过程中改变集合的长度（不能删除和增加）</strong>。</p> <p>如果要在迭代过程中删除元素，就不能使用集合的remove方法，只能使用迭代器的remove方法，此时只能使用迭代器来操作，不能使用foreach。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;西施&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;王昭君&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;貂蝉&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;杨玉环&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//获取迭代器对象</span>
<span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> it <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name">String</span> ele <span class="token operator">=</span> it<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token string">&quot;王昭君&quot;</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		it<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="八、set接口"><a href="#八、set接口" class="header-anchor">#</a> 八、Set接口</h2> <p>Set是Collection子接口，<strong>Set接口规定Set容器不记录元素的添加顺序，也不允许元素重复</strong>，那么Set接口的实现类都遵循这一种规则。</p> <p><strong>Set集合存储特点：</strong></p> <ul><li>不记录元素添加的先后顺序</li> <li>不允许元素重复</li></ul> <p>Set只包含从Collection继承的方法，不过Set无法记住添加的顺序，不允许包含重复的元素。当试图添加<strong>两个内容相同</strong>的元素进Set集合，添加操作失败，add()方法返回false。</p> <p>Set接口常用的实现类有：Set实现类命名规则：底层数据结构 + 实现接口</p> <ul><li><strong>HashSet类：底层采用哈希表实现，开发中使用最多的实现类，重点。</strong></li> <li>TreeSet类：底层采用红黑树实现，可以对集合中元素排序，使用不多。</li></ul> <p>Set 接口常用的方法</p> <ul><li>add()</li> <li>clear() / remove(E)</li> <li>size() / contains(E)</li> <li>iterator()</li></ul> <p>注意：Set 集合没有修改的方法</p> <h3 id="_8-1-hashset类"><a href="#_8-1-hashset类" class="header-anchor">#</a> 8.1 HashSet类</h3> <p>HashSet 是Set接口的实现类，底层数据结构是哈希表结构，HashSet容器不记录元素的添加顺序，也不允许元素重复。</p> <p>通常我们也说HashSet中的元素是<strong>无序的、唯一的</strong>。</p> <p>需求1：操作Set接口常用方法</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HashSetDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">// 添加操作：向列表中添加4个元素</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;Will&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;wolf&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;code&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">&quot;Lucy&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token comment">// 查询操作:</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;集合中所有元素：&quot;</span> <span class="token operator">+</span> set<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// [code, wolf, Will, Lucy]</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;元素数量：&quot;</span> <span class="token operator">+</span> set<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 4</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;是否存在某个元素：&quot;</span> <span class="token operator">+</span> set<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">&quot;code&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// true</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;是否存在某个元素：&quot;</span> <span class="token operator">+</span> set<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">&quot;code2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
        
		<span class="token comment">// 删除操作：删除code元素</span>
		set<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">&quot;code&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;删除后：&quot;</span> <span class="token operator">+</span> set<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// [wolf, Will, Lucy]</span>
		
        <span class="token comment">// 使用迭代器遍历</span>
		<span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> it <span class="token operator">=</span> set<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span> <span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token class-name">Object</span> ele <span class="token operator">=</span> it<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
        
        <span class="token comment">// 使用for-each遍历</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> ele <span class="token operator">:</span> set<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><h4 id="_8-1-1-对象的哈希值"><a href="#_8-1-1-对象的哈希值" class="header-anchor">#</a> 8.1.1 对象的哈希值</h4> <p><strong>哈希值（HashCode）</strong>：是JDK根据对象的地址或者对象的内容算出来的int类型的数值</p> <p>Object类中有一个方法可以获取对象的哈希值。public int hashCode():返回对象的哈希码值</p> <p><strong>对象的哈希值特点</strong></p> <ul><li>同一个对象多次调用hashCode()方法返回的哈希值是相同的</li> <li>默认情况下，不同对象的哈希值是不同的。而重写hashCode()方法，不同对象的哈希值有可能相同</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HashCodeDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token class-name">User</span> u <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>u<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 168423058</span>

        <span class="token comment">// 默认情况下，不同对象的哈希值是不同的。而重写hashCode()方法，不同对象的哈希值有可能相同</span>
        <span class="token class-name">User</span> u1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">User</span> u2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>u1<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>u2<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Aa&quot;</span><span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2112</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;BB&quot;</span><span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2112</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h4 id="_8-1-2-哈希表结构工作过程"><a href="#_8-1-2-哈希表结构工作过程" class="header-anchor">#</a> 8.1.2 哈希表结构工作过程</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blog02-hashset.png" alt=""></p> <p>HashSet 采用哈希表（）结构，向HashSet添加元素e时，经过以下过程：</p> <p>step 1: 首先计算要添加元素e的hashCode值。</p> <p>step 2：根据元素e的哈希值计算出e在哈希表的存储位置。e存储位置的计算方式为：存储位置 = hashcode % n</p> <p>step 3：根据存储位置查看哈希表该位置有无元素：</p> <ul><li>如果没有元素，直接添加该元素即可。</li> <li>如果有元素，看e元素和此位置所有元素是否内容相同 ( equals )
<ul><li>如果相同，则不添加</li> <li>如果不相同，在该位置连接e元素即可。</li></ul></li></ul> <p>可知，在哈希表中元素对象的hashCode和equals方法的很重要。</p> <p><strong>结论：</strong></p> <ul><li>存储到哈希表中的对象，都得覆盖hashCode和equals方法用来判断是否是同一个对象（特指内容相同）。</li> <li>一般的，根据对象的字段数据（内容）比较来判断，通常情况下equals为true的时候hashCode也应该相等。</li></ul> <p>需求2：创建三个User对象，覆盖equals和hashCode方法，存储在HashSet中。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>
	
    <span class="token comment">// 省略 getter、setter</span>
    <span class="token comment">// 省略 constructor</span>
	
	<span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">equals</span><span class="token punctuation">(</span><span class="token class-name">Object</span> o<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token operator">==</span> o<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>o <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> <span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> o<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>

        <span class="token class-name">User</span> user <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">User</span><span class="token punctuation">)</span> o<span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>age <span class="token operator">!=</span> user<span class="token punctuation">.</span>age<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> name <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">?</span> name<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>user<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token operator">:</span> user<span class="token punctuation">.</span>name <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> result <span class="token operator">=</span> name <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">?</span> name<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
        result <span class="token operator">=</span> <span class="token number">31</span> <span class="token operator">*</span> result <span class="token operator">+</span> age<span class="token punctuation">;</span>
        <span class="token keyword">return</span> result<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token string">&quot;User [name=&quot;</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">&quot;, age=&quot;</span> <span class="token operator">+</span> age <span class="token operator">+</span> <span class="token string">&quot;]&quot;</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><p>其中equals和hashCode方法表示根据User对象的name和age来做对比，两个方法是IDE工具自动生成的，不要求掌握。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HashSetDemo2</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">&gt;</span></span> users <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		users<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;will&quot;</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		users<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;lucy&quot;</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		users<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;stef&quot;</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		users<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;allen&quot;</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>users<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><blockquote><p>打完收工</p> <p>IDEA可以根据对象哪些字段做比较而自动生成hashCode和equals方法。</p></blockquote> <h3 id="_8-2-treeset类-了解"><a href="#_8-2-treeset类-了解" class="header-anchor">#</a> 8.2 TreeSet类（了解）</h3> <p>TreeSet类底层采用了红黑树（平衡二叉树）结构，会对存储的元素默认使用<strong>自然排序</strong>（从小到大）。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogtreeset-catalog.png" alt=""></strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TreeSetDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
         <span class="token comment">/**
         * TreeSet 底层采用平衡二叉树结构，实现了Set接口
         * TreeSet集合存储的特点
         * (1) 不记录元素添加顺序，不允许元素重复
         * (2) 会对添加的元素自然升序排序(从小到大)
         */</span>
		<span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>set<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// [2, 3, 5，6]</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// !!!提醒：问ChatGPT，String的排序规则</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p><strong>TreeSet工作过程</strong>：当向TreeSet添加元素e时</p> <p>step 1: 如果集合为空，直接添加元素，作为根节点</p> <p>step 2: 如果集合不为空，e和根节点比较</p> <ul><li>如果e 等于 根节点，e不能添加到集合中</li> <li>如果e 小于 根节点, e添加到根节点的左子树上，重复步骤1-2,过程中还要验证是否是平衡二叉树</li> <li>如果e 大于 根节点, e添加到根节点的右子树上，重复步骤1-2,过程中还要验证是否是平衡二叉树</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogimage-20230523150628795.png" alt=""></p> <h3 id="_8-3-comparable接口-知道"><a href="#_8-3-comparable接口-知道" class="header-anchor">#</a> 8.3 Comparable接口（知道）</h3> <p>默认情况下，TreeSet中的元素会采用自然排序(从小到大)</p> <p>此时要求元素对象必须实现java.lang.Comparable接口（言外之意：元素都可以比较大小），大多数JDK自带的类都实现了该接口，比如八大包装类和String。</p> <p>TreeSet会根据元素的compareTo方法来比较元素的大小关系,然后将集合元素按照升序排列。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Comparable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compareTo</span><span class="token punctuation">(</span><span class="token class-name">T</span> o<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>在compareTo中编写比较规则，拿当前元素和另一个元素o做比较：</p> <ul><li>this  &lt;    o：返回负整数  -1，存储在左边</li> <li>this  ==  o： 返回		     0，此时认为两个对象内容相同，元素不能加入集合中。</li> <li>this  &gt;    o：返回正整数   1，存储在右边</li></ul> <p>注意：this表示要添加的元素，另外一个元素o指根节点</p> <p>前面的案例中我们使用的是Integer，JDK已经实现了Comparable接口，并覆盖compareTo方法，在该方法编写按数组大小比较的比较规则</p> <p><strong>结论</strong>：如果我们自定义一个类，需要存储到TreeSet中，此时我们<strong>需要让该类实现Comparable接口，并覆盖compareTo方法，在该方法编写比较规则</strong>。</p> <p>需求：创建三个User对象，存入TreeSet中，要求按照用户的年龄从小到大排序</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">implements</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>Comparable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>
  
	<span class="token keyword">public</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
  
	<span class="token comment">// 比较规则:按照年龄比较</span>
	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compareTo</span><span class="token punctuation">(</span><span class="token class-name">User</span> o<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">&gt;</span> o<span class="token punctuation">.</span>age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">&lt;</span> o<span class="token punctuation">.</span>age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token string">&quot;User [name=&quot;</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">&quot;, age=&quot;</span> <span class="token operator">+</span> age <span class="token operator">+</span> <span class="token string">&quot;]&quot;</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ComparableDemo</span><span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 1 创建一个TreeSet集合</span>
		<span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">&gt;</span></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 2 创建三个User对象，添加到TreeSet中</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;stef&quot;</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;will&quot;</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;allen&quot;</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;lucy&quot;</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token class-name">User</span> u4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;coco&quot;</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>u4<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        
		<span class="token comment">// 3 打印输出集合元素</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>set<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br></div></div><p>输出结果：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token punctuation">[</span><span class="token class-name">User</span> <span class="token punctuation">[</span>name<span class="token operator">=</span>allen<span class="token punctuation">,</span> age<span class="token operator">=</span><span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name">User</span> <span class="token punctuation">[</span>name<span class="token operator">=</span>will<span class="token punctuation">,</span> age<span class="token operator">=</span><span class="token number">17</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name">User</span> <span class="token punctuation">[</span>name<span class="token operator">=</span>lucy<span class="token punctuation">,</span> age<span class="token operator">=</span><span class="token number">22</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name">User</span> <span class="token punctuation">[</span>name<span class="token operator">=</span>stef<span class="token punctuation">,</span> age<span class="token operator">=</span><span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>第一步：第一个节点进去，直接作为根节点，无需比较</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blognode-one.png" alt=""></strong></p> <p>第二步：第二个节点进去和第一个节点作比较，此时的this表示第二个节点，如果compareTo返回-1，则插入节点往左边走。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blognode-two.png" alt=""></strong></p> <p>第三步：第三个节点进去和第一个节点做比较，返回-1，再和第二个节点做比较。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blognode-three.png" alt=""></strong></p> <p>第四步：由于TreeSet是平衡二叉树，如果树不平衡，会对节点进行调整。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blognode-Four.png" alt=""></strong></p> <p>第五步：第四个节点进去先和will比较，再和stef比较。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blognode-five.png" alt=""></strong></p> <h3 id="_8-4-comparator接口-知道"><a href="#_8-4-comparator接口-知道" class="header-anchor">#</a> 8.4 Comparator接口（知道）</h3> <p>问题：我们已知道<strong>添加TreeSet的元素必须实现Comparable接口并覆盖compareTo编写比较规则</strong>。</p> <p>如果元素自身提供的比较规则不能满足我的需求怎么办？—— 指定比较器</p> <p>此时需要在构建TreeSet对象时传递java.util.Comparator接口的实现类对象，Comparator表示比较器，里面封装指定的比较规则。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">T</span> o1<span class="token punctuation">,</span> <span class="token class-name">T</span> o2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>比较规则，拿当前元素和另一个元素做比较：</p> <ul><li>o1  &lt;    o2：返回负整数  -1，存储在左边</li> <li>o1  ==  o2： 返回		     0，此时认为两个对象内容相同，元素不能加入集合中。</li> <li>o1  &gt;    o2：返回正整数   1，存储在右边</li></ul> <p>注意：o1指要添加的元素，o2代表根节点。</p> <p>需求：根据用户名长度升序排序，如果名字相同按照年龄升序排序。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ComparatorDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">&gt;</span></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">NameLengthComparator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;James&quot;</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;Bryant&quot;</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;Allen&quot;</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">&quot;Will&quot;</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>set<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">NameLengthComparator</span> <span class="token keyword">implements</span> <span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">User</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">User</span> o1<span class="token punctuation">,</span> <span class="token class-name">User</span> o2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">// 根据用户名长度升序排序</span>
        <span class="token keyword">int</span> len1 <span class="token operator">=</span> o1<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> len2 <span class="token operator">=</span> o2<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span> len1 <span class="token operator">==</span> len2 <span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 如果名字相同按照年龄升序排序</span>
            <span class="token keyword">return</span> o1<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> o2<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> len1 <span class="token operator">-</span> len2<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p>输出结果：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>[User [name=Will, age=17], User [name=Allen, age=28], User [name=James, age=30], User [name=Bryant, age=22]]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>小结：</p> <p>HashSet做等值查询效率高，TreeSet做范围查询效率高，在开发中一般使用HashSet就可以了。</p> <h2 id="九、map接口"><a href="#九、map接口" class="header-anchor">#</a> 九、Map接口</h2> <h3 id="_9-1-map接口概述-理解"><a href="#_9-1-map接口概述-理解" class="header-anchor">#</a> 9.1 Map接口概述（理解）</h3> <p>Set和List是单元素集合，Map是双元素集合。</p> <ul><li>单元素集合：每次只能存储一个元素，比如Set和List。</li> <li>双元素集合：每次需要存储两个元素（一个key和一个value），比如Map。</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/blogimage-20230304130209440.png" alt=""></p> <p>java.util.Map&lt;K,V&gt; 集合，Map&lt;K,V&gt; <code>K</code> : 键的数据类型，<code>V</code> : 值的数据类型，</p> <p>里面保存的数据是成对儿存在的，可以看做双列集合。</p> <p><strong>Map结构的特点：</strong></p> <ul><li>键不能重复，值可以重复</li> <li>键和值是一一对应的，通过键可以找到对应的值</li> <li>(键+值)一起是一个整体 我们称之为 &quot;键值对&quot; 或者 &quot;键值对对象&quot;，经常被称为 key =&gt; value 对</li></ul> <p>注意：</p> <ul><li><p>Map接口并没有继承于Collection接口也没有继承于Iterable接口，所以不能直接对Map使用for-each操作。</p></li> <li><p><strong>如果不能理解Map的结构，就直接记住Map每次需要存储两个值，一个是key，一个是value，其中value表示存储的数据，而key就是这一个value的名字</strong>。</p></li></ul> <p>Map接口的实现类</p> <ul><li><strong>HashMap类：底层采用哈希表实现，开发中使用最多的实现类，重点。</strong></li> <li>TreeMap类：底层采用红黑树实现，可以对key排序，使用不多。</li></ul> <h3 id="_9-2-map常用的api方法-记住"><a href="#_9-2-map常用的api方法-记住" class="header-anchor">#</a> 9.2 Map常用的API方法（记住）</h3> <p>添加操作</p> <ul><li><p><font color="red">boolean put(Object key,Object value)：存储一个键值对到Map中</font></p></li> <li><p>boolean putAll(Map  m)：把m中的所有键值对添加到当前Map中</p></li></ul> <p>删除操作</p> <ul><li><font color="red">Object remove(Object key)：从Map中删除指定key的键值对，并返回被删除key对应的value</font></li></ul> <p>修改操作</p> <ul><li>无专门的方法，可以调用put方法，存储相同key，不同value的键值对，可以覆盖原来的。</li></ul> <p>查询操作</p> <ul><li><p>int size()：返回当前Map中键值对个数</p></li> <li><p>boolean isEmpty()：判断当前Map中键值对个数是否为0.</p></li> <li><p><font color="red">Object get(Object key)：返回Map中指定key对应的value值，如果不存在该key，返回null</font></p></li> <li><p>boolean containsKey(Object key):判断Map中是否包含指定key</p></li> <li><p>boolean containsValue(Object value):判断Map中是否包含指定value</p></li> <li><p><font color="red">Set keySet()：返回Map中所有key所组成的Set集合</font></p></li> <li><p><font color="red">Collection values()：返回Map中所有value所组成的Collection集合</font></p></li></ul> <p>注意，标红的是重度使用的方法。</p> <h3 id="_9-3-hashmap-重点"><a href="#_9-3-hashmap-重点" class="header-anchor">#</a> 9.3 HashMap（重点）</h3> <p>HashMap底层基于哈希表算法，Map中存储的key对象的hashCode值决定了在哈希表中的存储位置（<strong>HashMap key的底层数据结构是哈希表</strong>），所以不能保证添加的先后顺序，也不允许重复。</p> <p>需求1：操作Map接口常用方法</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HashMapDemo1</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">// 添加元素</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;girl1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;西施&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;girl2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;王昭君&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;girl3&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;貂蝉&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;girl4&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;杨玉环&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;map中有多少键值对：&quot;</span> <span class="token operator">+</span> map<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token comment">// 查询操作</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;获取key为girl1的值:&quot;</span> <span class="token operator">+</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;girl1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;是否包含key为girl1：&quot;</span><span class="token operator">+</span>map<span class="token punctuation">.</span><span class="token function">containsKey</span><span class="token punctuation">(</span><span class="token string">&quot;girl1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;是否包含value为貂蝉：&quot;</span><span class="token operator">+</span>map<span class="token punctuation">.</span><span class="token function">containsValue</span><span class="token punctuation">(</span><span class="token string">&quot;貂蝉&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
		<span class="token comment">// 替换key为girl3的value值</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;girl3&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;小乔&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token comment">// 删除key为girl3的键值对</span>
		map<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">&quot;girl3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>Map的迭代遍历：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 获取Map中所有的key</span>
<span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> keys <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Map中所有key：&quot;</span> <span class="token operator">+</span> keys<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 获取Map中所有的value</span>
<span class="token class-name">Collection</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> values <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Map中所有value：&quot;</span> <span class="token operator">+</span> values<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 通过key找对应的value</span>
<span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> keySet <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//  1 获取所有的key</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> key <span class="token operator">:</span> keySet<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 2 通过key找value</span>
    <span class="token class-name">String</span> val <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>key <span class="token operator">+</span> <span class="token string">&quot;=&gt;&quot;</span> <span class="token operator">+</span> val<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 注意: map不支持for-each操作</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>需求：统计一个字符串中每个字符出现次数</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HashMapDemo2</span><span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">&quot;ABCDEFABCDEABCDABCABA&quot;</span><span class="token punctuation">;</span>
		<span class="token comment">// 把字符串转换为char数组</span>
		<span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> charArray <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">// Map的key存储字符，value存储出现的次数</span>
		<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Character</span><span class="token punctuation">,</span> <span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">// 迭代每一个字符</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">char</span> ch <span class="token operator">:</span> charArray<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token comment">// 判断Map中是否已经存储该字符</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span>map<span class="token punctuation">.</span><span class="token function">containsKey</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				<span class="token class-name">Integer</span> count <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token comment">// 如果已经存储该字符，则把出现次数加上1</span>
				map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> count<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>
				<span class="token comment">// 如果没有存储该字符，则把设置次数为1</span>
				map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
		<span class="token punctuation">}</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span> 
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="_9-4-treemap-了解"><a href="#_9-4-treemap-了解" class="header-anchor">#</a> 9.4 TreeMap（了解）</h3> <p>TreeMap集合 key采用平衡二叉树结构，TreeMap中存储的key会默认使用自然排序（从小到大）</p> <p>和TreeSet一样，除了可以使用自然排序也可以指定比较器</p> <p>需求：测试HashMap和TreeMap中key的顺序</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TreeMapDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;girl4&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;杨玉环&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;girl2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;王昭君&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;key1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;西施&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;key3&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;貂蝉&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token comment">//-------------------------------------------</span>
		map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;girl4&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;杨玉环&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;girl2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;王昭君&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;key1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;西施&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;key3&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;貂蝉&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>输出结果：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>{key1=西施, girl4=杨玉环, key3=貂蝉, girl2=王昭君}
{girl2=王昭君, girl4=杨玉环, key1=西施, key3=貂蝉}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="十、框架小结和工具类"><a href="#十、框架小结和工具类" class="header-anchor">#</a> 十、框架小结和工具类</h2> <h3 id="_10-1-框架小结"><a href="#_10-1-框架小结" class="header-anchor">#</a> 10.1 框架小结</h3> <p>List、Set、Map选用</p> <p>一般的在存储元素时候，是否需要给元素起一个名字：</p> <ul><li><p>需要：此时使用Map。</p></li> <li><p>不需：存储的元素使用需要保证添加的先后顺序</p> <ul><li>需要：此时使用List</li> <li>不需：此时使用Set（如果需要保证集合元素不重复，也选用Set）</li></ul></li></ul> <p>2&gt; 根据具体业务场景选择这些接口具体的实现类。</p> <h3 id="_10-2-工具类"><a href="#_10-2-工具类" class="header-anchor">#</a> 10.2 工具类</h3> <h4 id="_10-2-1-工具类的设计"><a href="#_10-2-1-工具类的设计" class="header-anchor">#</a> 10.2.1 工具类的设计</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 验证手机号码的功能</span>
<span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">isPhone</span><span class="token punctuation">(</span><span class="token class-name">String</span> str<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>一般地，把那些完成<strong>通用功能的方法</strong>分类存放到类中，这些类就叫工具类。</p> <ul><li>工具类起名：XxxUtil、XxxUtils、XxxTool、XxxTools等，其中Xxx表示一类事物，比如ArrayUtil、StringUtil、JdbcUtil。</li> <li>工具类存放的包起名：util、utils、tool、tools等</li> <li>工具类在开发中的应用场景 ：作为工具性质且能高效地重复使用。</li></ul> <p>经常使用的<strong>设计工具类的方式是使用公共静态方法</strong></p> <ul><li><p>此时必须把工具类的构造器私有化，防止创建工具类的对象来调用静态方法</p></li> <li><p>只需要使用工具类名调用工具方法</p></li></ul> <p>比如使用公共静态方法的方式，设计一个数组的工具类。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayUtils</span> <span class="token punctuation">{</span>

    <span class="token comment">// 1 构造器私有化</span>
    <span class="token keyword">private</span> <span class="token class-name">ArrayUtils</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>

    <span class="token comment">// 2 提供public static 方法作为工具方法</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">int</span> total <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> ele<span class="token operator">:</span>arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            total <span class="token operator">+=</span> ele<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> total<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>调用者直接使用 <code>工具类名.工具方法(实参)</code> 完成调用：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="_10-2-2-arrays-掌握"><a href="#_10-2-2-arrays-掌握" class="header-anchor">#</a> 10.2.2 Arrays（掌握）</h4> <p>Arrays类是数组的工具类，提供了对打印数组元素，排序，查找等功能，除此之外，我们还经常会遇到的需求：把数组转成集合</p> <ul><li>public static &lt;占位符&gt; List&lt;占位符&gt; asList(T... a)：该方法可以把一个Object数组转换为List集合。</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArraysDemo</span><span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">// 把Integer[]转换为List&lt;Integer&gt;</span>
		<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> list1 <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>list1<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token comment">// 把String[]转换为List&lt;String&gt;</span>
		<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list2 <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;B&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;C&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>list2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
    	<span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> strArr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">&quot;Hello1&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;Hello2&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;Hello3&quot;</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    	<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list3 <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span>strArr<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>注意：通过Arrays.asList方法得到的List对象的长度是固定的，不能增，也不能减。</p> <h4 id="_10-2-3-collections-了解"><a href="#_10-2-3-collections-了解" class="header-anchor">#</a> 10.2.3 Collections（了解）</h4> <p>Collections是集合的工具类，封装了Set、List、Map操作的工具方法，比如拷贝、排序、搜索、比较大小等。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;wolf&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 需求1: 按照字符串大小顺序升序排序</span>
<span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 需求2: 按照字符串的长度升序排序</span>
<span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">String</span> o1<span class="token punctuation">,</span> <span class="token class-name">String</span> o2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> o1<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> o2<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;list列表元素:&quot;</span> <span class="token operator">+</span> list3<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="十一、小结与作业"><a href="#十一、小结与作业" class="header-anchor">#</a> 十一、小结与作业</h2> <h3 id="_11-1-小结"><a href="#_11-1-小结" class="header-anchor">#</a> 11.1 小结</h3> <ul><li>了解数据结构-数组+链表+队列+栈</li> <li>掌握集合框架体系</li> <li>掌握List接口体系与常用API</li> <li>掌握List体系实现类-ArrayList+LinkedList+Vector集合使用-建议5遍</li> <li>掌握Set体系实现类-HashSet+TreeSet集合使用-建议5遍</li> <li>掌握Map体系实现类-HashMap+TreeMap集合使用-建议5遍</li> <li>掌握集合体系中泛型、比较接口使用-建议5遍</li> <li>掌握集合体系接口选择与适用范围</li> <li>掌握常用的集合工具类-Arrays+Collections-建议5遍</li></ul> <h3 id="_11-2-作业"><a href="#_11-2-作业" class="header-anchor">#</a> 11.2 作业</h3> <h4 id="_11-2-1-题目1"><a href="#_11-2-1-题目1" class="header-anchor">#</a> 11.2.1 题目1</h4> <p>需求：演示List迭代的三种方式</p> <p>作业目的：掌握List迭代的三种方式</p> <h4 id="_11-2-2-题目2"><a href="#_11-2-2-题目2" class="header-anchor">#</a> 11.2.2 题目2</h4> <p>需求：演示List中的并发修改异常</p> <p>作业目的：</p> <p>记住结论：迭代器迭代只能使用迭代器的remove方法</p> <p>集合遍历只能使用集合的remove方法</p> <h4 id="_11-2-3-题目3"><a href="#_11-2-3-题目3" class="header-anchor">#</a> 11.2.3 题目3</h4> <p>需求：演示HashSet接口中的常用操作</p> <p>增：</p> <p>删：</p> <p>改：</p> <p>查：</p> <p>遍历</p> <p>作业目的：掌握HashSet接口中的常用方法的使用和遍历</p> <h4 id="_11-2-4-题目4"><a href="#_11-2-4-题目4" class="header-anchor">#</a> 11.2.4 题目4</h4> <p>需求：创建三个User对象，覆盖equals和hashCode方法，存储在HashSet中。</p> <p>作业目的：了解HashSet添加元素过程，以及HashSet是如何判断元素相同的</p> <h4 id="_11-2-5-题目5"><a href="#_11-2-5-题目5" class="header-anchor">#</a> 11.2.5 题目5</h4> <p>需求：演示TreeSet接口中的常用操作</p> <p>增：</p> <p>删：</p> <p>改：</p> <p>查：</p> <p>遍历</p> <p>作业目的：掌握TreeSet接口中的常用方法的使用和遍历</p> <h4 id="_11-2-6-题目6"><a href="#_11-2-6-题目6" class="header-anchor">#</a> 11.2.6 题目6</h4> <p>需求：使用Comparable接口按照用户的年龄从小到大排序</p> <p>步骤：</p> <ul><li><p>定义User类，并实现Comparable接口，覆盖compareTo方法</p></li> <li><p>创建TreeSet对象，将User对象添加进TreeSet对象中</p></li></ul> <p>作业目的：了解Comparable的作用</p> <h4 id="_11-2-7-题目7"><a href="#_11-2-7-题目7" class="header-anchor">#</a> 11.2.7 题目7</h4> <p>需求：使用Comparator接口根据用户的名字长度排序，如果名字相同按照年龄排序。</p> <p>步骤：</p> <ul><li><p>定义一个NameComparator类实现Comparator接口</p></li> <li><p>定义User类，作为TreeSet的元素类型</p></li> <li><p>创建TreeSet对象，并传入一个NameComparator比较器对象作为TreeSet构造器的参数</p></li></ul> <p>作业目的：了解Comparator的作用</p> <h4 id="_11-2-8-题目8"><a href="#_11-2-8-题目8" class="header-anchor">#</a> 11.2.8 题目8</h4> <p>需求：调用HashMap的常用方法</p> <p><strong>添加操作</strong></p> <ul><li><p>boolean put(Object key,Object value)：存储一个键值对到Map中</p></li> <li><p>boolean putAll(Map  m)：把m中的所有键值对添加到当前Map中</p></li></ul> <p><strong>删除操作</strong></p> <ul><li>Object remove(Object key)：从Map中删除指定key的键值对，并返回被删除key对应的value</li></ul> <p><strong>修改操作</strong></p> <ul><li>无专门的方法，可以调用put方法，存储相同key，不同value的键值对，可以覆盖原来的。</li></ul> <p><strong>查询操作</strong></p> <ul><li><p>int size()：返回当前Map中键值对个数</p></li> <li><p>boolean isEmpty()：判断当前Map中键值对个数是否为0.</p></li> <li><p>Object get(Object key)：返回Map中指定key对应的value值，如果不存在该key，返回null</p></li> <li><p>boolean containsKey(Object key):判断Map中是否包含指定key</p></li> <li><p>boolean containsValue(Object value):判断Map中是否包含指定value</p></li> <li><p>Set keySet()：返回Map中所有key所组成的Set集合</p></li> <li><p>Collection values()：返回Map中所有value所组成的Collection集合</p></li> <li><p>Set entrySet()：返回Map中所有键值对所组成的Set集合</p></li></ul> <p>作业目的：掌握HashMap常用方法的使用</p> <h4 id="_11-2-9-题目9"><a href="#_11-2-9-题目9" class="header-anchor">#</a> 11.2.9 题目9</h4> <p>需求：演示HashMap的迭代操作（三个）</p> <p>作业目的：掌握HashMap的三个迭代操作</p> <h4 id="_11-2-10-题目10"><a href="#_11-2-10-题目10" class="header-anchor">#</a> 11.2.10 题目10</h4> <p>需求：统计一个字符串中每个字符出现次数</p> <h4 id="_11-2-11-题目11"><a href="#_11-2-11-题目11" class="header-anchor">#</a> 11.2.11 题目11</h4> <p>需求：调用Arrays和Collections工具类中的常用方法</p> <p>作业目的：了解Arrays和Collections工具类中的常用方法</p> <h4 id="_11-2-12-题目12"><a href="#_11-2-12-题目12" class="header-anchor">#</a> 11.2.12 题目12</h4> <p>集合使用.</p> <p>①写一个点类Point, 有X,Y坐标.</p> <p>②创建5个Point对象, 添加到Set集合HashSet中</p> <p>⑤遍历Set集合对象并打印输出该Set对象</p> <h4 id="_11-2-13-题目13"><a href="#_11-2-13-题目13" class="header-anchor">#</a> 11.2.13 题目13</h4> <p>有如下Student对象，对象中包含姓名（name）、年龄（age）、成绩（score）、班级号（classNum）等成员变量，其中，classNum表示学生的班号，例如“class05”。</p> <p>学生信息表数据如下：</p> <table><thead><tr><th>序号</th> <th>姓名</th> <th>年龄</th> <th>成绩</th> <th>班级号</th> <th>序号</th> <th>姓名</th> <th>年龄</th> <th>成绩</th> <th>班级号</th></tr></thead> <tbody><tr><td>1</td> <td>Tom</td> <td>18</td> <td>100</td> <td>class05</td> <td>4</td> <td>Jim</td> <td>30</td> <td>80</td> <td>class05</td></tr> <tr><td>2</td> <td>Jerry</td> <td>22</td> <td>70</td> <td>class04</td> <td>5</td> <td>Steve</td> <td>28</td> <td>66</td> <td>class06</td></tr> <tr><td>3</td> <td>Owen</td> <td>25</td> <td>90</td> <td>class05</td> <td>6</td> <td>Kevin</td> <td>24</td> <td>100</td> <td>class04</td></tr></tbody></table> <p>请根据学生信息表，完成下列要求：</p> <p>1）创建HashSet对象stuSet，并根据学生信息表添加学生数据。</p> <p>2）修改Jerry的成绩将70修改为90。</p> <p>3）删除Kevin这一条学生数据。</p> <p>4）计算class05班级的平均分。</p> <h4 id="_11-2-14-题目14"><a href="#_11-2-14-题目14" class="header-anchor">#</a> 11.2.14 题目14</h4> <p>已知java学院教课内容安排如下</p> <p>老师    课程</p> <p>stef	   p2p</p> <p>will    javaWeb</p> <p>neld    springmvc</p> <p>hesj    wms</p> <p>bunny   javaWeb</p> <p>hhaiy   javaSe</p> <p>要求：</p> <p>1：使用Map， 以老师名字为key 课程名为value， 完成课程安排</p> <p>2：添加一个新老师 dafei 教jdbc</p> <p>3：hhaiy 改为教javaWeb</p> <p>4：输出所有老师，以及老师教授课程</p> <p>5：查出所有教javaWeb的老师</p> <h4 id="_11-2-15-题目15"><a href="#_11-2-15-题目15" class="header-anchor">#</a> 11.2.15 题目15</h4> <p>分别创建员工类（Employee）和部门类（Department），其中员工包含员工姓名（name），年龄(age)。部门类中含有部门名称（deptName），每个部门中含有一个集合（emps）表示一个部门中含有多个员工，每个员工是是唯一的，无需记录添加顺序。</p> <p>给出以下部门信息表：</p> <table><thead><tr><th>部门名称</th> <th>员工姓名</th> <th>员工年龄</th></tr></thead> <tbody><tr><td><strong>部门1</strong></td> <td>小斌</td> <td>22</td></tr> <tr><td></td> <td>地雷</td> <td>23</td></tr> <tr><td></td> <td>吉吉</td> <td>21</td></tr> <tr><td><strong>部门2</strong></td> <td>东方菇凉</td> <td>18</td></tr> <tr><td></td> <td>独孤求败</td> <td>28</td></tr> <tr><td></td> <td>糖糖</td> <td>25</td></tr></tbody></table> <p>要求：</p> <p>​	1）按照JavaBean规范定义员工类（Employee）和部门类（Department）。</p> <p>​	2）使用有参构造器，根据员工信息表创建部门1和部门2两个对象。</p> <p>巩固二</p> <h4 id="_11-2-16-题目16"><a href="#_11-2-16-题目16" class="header-anchor">#</a> 11.2.16 题目16</h4> <ol><li>创建一个List，在List 中增加三个工人，基本信息如下：</li></ol> <p>​	姓名 		年龄 		工资</p> <p>​	zhang3 	18 		  3000</p> <p>​	li4 			 25 		  3500</p> <p>​	wang5 	 22 		  3200</p> <ol start="2"><li>在li4 之前插入一个工人，信息为：</li></ol> <p>姓名：zhao6，年龄：24，工资3300</p> <ol start="3"><li><p>删除wang5 的信息</p></li> <li><p>利用for 循环遍历，打印List 中所有工人的信息</p></li> <li><p>利用迭代遍历，对List 中所有的工人调用work 方法。</p></li> <li><p>为Worker 类重写equals 方法，当姓名、年龄、工资全部相等时候才返回true</p></li></ol></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=list" title="标签">#list</a><a href="/tags/?tag=set" title="标签">#set</a><a href="/tags/?tag=map" title="标签">#map</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/36ed35/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">常用类-数组高级</div></a> <a href="/pages/19e70b/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">异常机制</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/36ed35/" class="prev">常用类-数组高级</a></span> <span class="next"><a href="/pages/19e70b/">异常机制</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/24.c1af1909.js" defer></script>
  </body>
</html>
